import { message } from 'ant-design-vue';

import { generateWechatQrCode } from '#/api/wechat';
import { downloadImage } from '#/utils/file/download';

export async function getMiniProQrcode(
  appId: string,
  path: string,
  width?: number,
) {
  return (
    (await generateWechatQrCode({
      appid: appId,
      path,
      width: width || 1280,
      autoColor: true,
      isHyaline: false,
      envVersion: 'release',
    })) ?? null
  );
}

/**
 * 下载小程序码
 * @param appId
 * @param path
 * @param filename
 */
export async function downloadMiniProQrcode(
  appId: string,
  path: string,
  filename?: string,
) {
  const hideLoading = message.loading('下载中', 0);
  try {
    const data = await getMiniProQrcode(appId, path);
    if (data?.includes('http')) {
      downloadImage(data, filename);
    } else {
      message.error('小程序码生成失败');
    }
  } catch (error) {
    console.error(error);
  } finally {
    hideLoading();
  }
}

/**
 * 下载工业展小程序码
 * @param path
 * @param filename
 */
export async function downloadGYZMiniProQrcode(
  path: string,
  filename?: string,
) {
  await downloadMiniProQrcode('wx59f92e73eadf2260', path, filename);
}

/**
 * 下载易展通小程序码
 * @param path
 * @param filename
 */
export async function downloadYZTMiniProQrcode(
  path: string,
  filename?: string,
) {
  await downloadMiniProQrcode('wx81f3f3a7a3bda3c8', path, filename);
}

/**
 * 下载易会通小程序码
 * @param path
 * @param filename
 */
export async function downloadYHTMiniProQrcode(
  path: string,
  filename?: string,
) {
  await downloadMiniProQrcode('wxa5d8f046f32f6514', path, filename);
}
